/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.smarttmt.smartmanager.bean;

import java.util.List;
import java.util.Map;
import javax.faces.event.ValueChangeListener;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;
import org.smarttmt.smartmanager.controller.ActividaJpaController;
import org.smarttmt.smartmanager.controller.SolucionJpaController;
import org.smarttmt.smartmanager.controller.VwActividadesSolicitudJpaController;
import org.smarttmt.smartmanager.controller.VwSolicitudesJpaController;
import org.smarttmt.smartmanager.model.ActividadDetalleM;
import org.smarttmt.smartmanager.model.ActividadM;
import org.smarttmt.smartmanager.model.SolicitudM;
import org.smarttmt.smartmanager.model.SolucionM;

/**
 *
 * @author nicolas
 */
@javax.enterprise.context.SessionScoped
@javax.inject.Named(value = "customerOperationsBean")
public class CustomerOperations implements java.io.Serializable, org.smarttmt.smartmanager.behavior.Preloadable {

    private java.util.List<org.smarttmt.smartmanager.model.ActividadM> actividadList;
    private java.util.List<org.smarttmt.smartmanager.model.SolucionM> solucionList;
    private java.util.List<org.smarttmt.smartmanager.model.SolicitudM> solicitudList;
    private java.util.List<org.smarttmt.smartmanager.model.ActividadDetalleM> actividadDetalleList;
    @javax.annotation.Resource
    javax.transaction.UserTransaction utx;
    @javax.persistence.PersistenceUnit(unitName = "org.smarttmt_SMARTManager_war_1.0-SNAPSHOTPU")
    javax.persistence.EntityManagerFactory emf;
    private org.smarttmt.smartmanager.controller.ActividaJpaController actividaJpaController;
    private org.smarttmt.smartmanager.controller.SolucionJpaController solucionJpaController;
    private org.smarttmt.smartmanager.controller.VwSolicitudesJpaController vwSolicitudesJpaController;
    private org.smarttmt.smartmanager.controller.VwActividadesSolicitudJpaController vwActividadesSolicitudJpaController;
    private boolean detail;
    private String prinxNumber;

    public CustomerOperations() {
    }

    @javax.annotation.PostConstruct
    public void initCustomerOperations() {
        actividaJpaController = new ActividaJpaController(utx, emf);
        solucionJpaController = new SolucionJpaController(utx, emf);
        vwSolicitudesJpaController = new VwSolicitudesJpaController(utx, emf);
        vwActividadesSolicitudJpaController = new VwActividadesSolicitudJpaController(utx, emf);
    }

    public void inquireActivityDetail() {

            actividadDetalleList = new java.util.ArrayList<>();

        try {
            java.util.List<org.smarttmt.smartmanager.entity.VwActividadesSolicitud> result = vwActividadesSolicitudJpaController.inquireActivityDetail(Integer.parseInt(prinxNumber));
            java.util.Iterator<org.smarttmt.smartmanager.entity.VwActividadesSolicitud> it = result.iterator();
            org.smarttmt.smartmanager.entity.VwActividadesSolicitud actisoliAux;

            while (it.hasNext()) {
                actisoliAux = it.next();
                org.smarttmt.smartmanager.model.ActividadDetalleM actDeta = new ActividadDetalleM();
                actDeta.actividaDetalleMapped(actisoliAux);
                actividadDetalleList.add(actDeta);
            }
        } catch (NumberFormatException e) {
            System.out.println(toString());
        }
    }

    @Override
    public void preload(Map<String, Object> parameters) {
        resetData();
        /*
         if(!parameters.isEmpty()){
         prinxNumber = (String) parameters.get("prinxNumber");
         }
         */
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        //loadActivities();
        //loadSoluciones();
        loadSolicitudes();

        //toString();
    }
    
    public String cleanPrinxNumber(){
        prinxNumber = null;
        detail = false;
        actividadDetalleList = null;
        solicitudList = null;
        loadSolicitudes();
        return null;
    }

    public void resetData() {
        solicitudList = null;
        solucionList = null;
        actividadList = null;
        detail = false;
        prinxNumber = null;
        actividadDetalleList = null;
    }

    public void prinxSelected(String prinxNumber) {
        
         if (prinxNumber != null) {
         this.prinxNumber = prinxNumber;
         detail = true;
         } else {
         detail = false;
         }
         

        this.prinxNumber = prinxNumber;
        System.out.println(toString());
        if (this.prinxNumber != null) {
            //searchChangeListener(null);
            inquireActivityDetail();
        } else {
            loadSolicitudes();
        }
    }

    public void searchChangeListener(ValueChangeListener eve) {
        solicitudList = null;
        loadSolicitudes(this.prinxNumber);
    }

    public void loadSolicitudes(String prinxNumber) {
        if (solicitudList == null) {
            solicitudList = new java.util.ArrayList<>();
        }

        try {
            List<org.smarttmt.smartmanager.entity.VwSolicitudes> result = vwSolicitudesJpaController.findVwSolicitudesEntitiesByAllParams(prinxNumber);
            java.util.Iterator<org.smarttmt.smartmanager.entity.VwSolicitudes> it = result.iterator();
            org.smarttmt.smartmanager.entity.VwSolicitudes solicitudAux;

            while (it.hasNext()) {
                solicitudAux = it.next();
                //System.out.println("SolicitudEntity: ------>" + solicitudAux.toString());
                org.smarttmt.smartmanager.model.SolicitudM solicitud = new SolicitudM();
                solicitud.solicitudMapped(solicitudAux);
                //System.out.println("SolicitudM: ----------->" + solicitud.toString());
                solicitudList.add(solicitud);
            }
        } catch (Exception e) {
        }
    }

    public void loadSolicitudes() {
        if (solicitudList == null) {
            solicitudList = new java.util.ArrayList<>();
        }

        try {
            List<org.smarttmt.smartmanager.entity.VwSolicitudes> result = vwSolicitudesJpaController.findVwSolicitudesEntities();
            java.util.Iterator<org.smarttmt.smartmanager.entity.VwSolicitudes> it = result.iterator();
            org.smarttmt.smartmanager.entity.VwSolicitudes solicitudAux;

            while (it.hasNext()) {
                solicitudAux = it.next();
                //System.out.println("SolicitudEntity: ------>" + solicitudAux.toString());
                org.smarttmt.smartmanager.model.SolicitudM solicitud = new SolicitudM();
                solicitud.solicitudMapped(solicitudAux);
                //System.out.println("SolicitudM: ----------->" + solicitud.toString());
                solicitudList.add(solicitud);
            }
        } catch (Exception e) {
        }

    }

    public void loadSoluciones() {
        if (solucionList == null) {
            solucionList = new java.util.ArrayList<>();
        }

        try {
            List<org.smarttmt.smartmanager.entity.Solucion> result = solucionJpaController.findSolucionEntities();
            java.util.Iterator<org.smarttmt.smartmanager.entity.Solucion> it = result.iterator();
            org.smarttmt.smartmanager.entity.Solucion solucionAux;

            while (it.hasNext()) {
                solucionAux = it.next();
                org.smarttmt.smartmanager.model.SolucionM solucion = new SolucionM();
                solucion.solucionMapped(solucionAux);
                solucionList.add(solucion);
            }
        } catch (Exception e) {
        }
    }

    public void loadActivities() {
        if (actividadList == null) {
            actividadList = new java.util.ArrayList<>();
        }

        try {
            List<org.smarttmt.smartmanager.entity.Activida> result = actividaJpaController.findActividaEntities();
            java.util.Iterator<org.smarttmt.smartmanager.entity.Activida> it = result.iterator();
            org.smarttmt.smartmanager.entity.Activida actividaAux;

            while (it.hasNext()) {
                actividaAux = it.next();
                org.smarttmt.smartmanager.model.ActividadM actividad = new ActividadM();
                actividad.actividaMapped(actividaAux);
                actividadList.add(actividad);
            }

        } catch (Exception e) {
        }

    }

    public List<ActividadM> getActividadList() {
        return actividadList;
    }

    public void setActividadList(List<ActividadM> actividadList) {
        this.actividadList = actividadList;
    }

    public UserTransaction getUtx() {
        return utx;
    }

    public void setUtx(UserTransaction utx) {
        this.utx = utx;
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public ActividaJpaController getActividaJpaController() {
        return actividaJpaController;
    }

    public void setActividaJpaController(ActividaJpaController actividaJpaController) {
        this.actividaJpaController = actividaJpaController;
    }

    public List<SolucionM> getSolucionList() {
        return solucionList;
    }

    public void setSolucionList(List<SolucionM> solucionList) {
        this.solucionList = solucionList;
    }

    public SolucionJpaController getSolucionJpaController() {
        return solucionJpaController;
    }

    public void setSolucionJpaController(SolucionJpaController solucionJpaController) {
        this.solucionJpaController = solucionJpaController;
    }

    public List<SolicitudM> getSolicitudList() {
        return solicitudList;
    }

    public void setSolicitudList(List<SolicitudM> solicitudList) {
        this.solicitudList = solicitudList;
    }

    public VwSolicitudesJpaController getVwSolicitudesJpaController() {
        return vwSolicitudesJpaController;
    }

    public void setVwSolicitudesJpaController(VwSolicitudesJpaController vwSolicitudesJpaController) {
        this.vwSolicitudesJpaController = vwSolicitudesJpaController;
    }

    public boolean isDetail() {
        return detail;
    }

    public void setDetail(boolean detail) {
        this.detail = detail;
    }

    public String getPrinxNumber() {
        return prinxNumber;
    }

    public void setPrinxNumber(String prinxNumber) {
        this.prinxNumber = prinxNumber;
    }

    public List<ActividadDetalleM> getActividadDetalleList() {
        return actividadDetalleList;
    }

    public void setActividadDetalleList(List<ActividadDetalleM> actividadDetalleList) {
        this.actividadDetalleList = actividadDetalleList;
    }

    public VwActividadesSolicitudJpaController getVwActividadesSolicitudJpaController() {
        return vwActividadesSolicitudJpaController;
    }

    public void setVwActividadesSolicitudJpaController(VwActividadesSolicitudJpaController vwActividadesSolicitudJpaController) {
        this.vwActividadesSolicitudJpaController = vwActividadesSolicitudJpaController;
    }

    @Override
    public String toString() {
        return "CustomerOperations{" + "detail=" + detail + ", prinxNumber=" + prinxNumber + '}';
    }

}
